package org.lalib.entity.object.variable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.model.SelectItem;

import org.lalib.entity.object.variable.value.DropDownVariableBean;
import org.lalib.util.VariableTypes;

public class DropDownListVariable extends EVariable {

	private static final long serialVersionUID = 1L;

	private Map<String, String> choices = new HashMap<>();

	public List<SelectItem> choiseSelectItems() {
		List<SelectItem> selectItems = new ArrayList<>();
		for (Map.Entry<String, String> item : choices.entrySet())
			selectItems.add(new SelectItem(item.getKey(), item.getValue()));
		return selectItems;
	}

	public List<String> choiseKeys() {
		List<String> keys = new ArrayList<String>();
		if (choices != null) {
			Set<String> keyset = choices.keySet();
			keys.addAll(keyset);
		}
		return keys;
	}

	@Override
	public String getType() {
		return VariableTypes.DROPDOWNLISTVARIABLE;
	}

	@Override
	public Class<?> getVariableClass() {
		return DropDownListVariable.class;
	}

	@Override
	public Object getEmptyCore() {
		return new DropDownVariableBean();
	}

	public Map<String, String> getChoices() {
		return choices;
	}

	public void setChoices(Map<String, String> choices) {
		this.choices = choices;
	}

	public void addChoice(String dropKey, String dropValue) {
		choices.put(dropKey, dropValue);
	}

	public void removeChoice(String key) {
		choices.remove(key);
	}
}